<?php
//  +----------------------------------------------------------------------
//  | Copyright (c) 2015-2025 http://www.hdphp.cn All rights reserved.
//  | Licensed ( http://www.hdphp.cn/licenses/ )
//  | Author: Jack <sophia2152@qq.com>
//  | 官方网址: http://www.hdphp.cn
//  | 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用。
//  | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
//  +----------------------------------------------------------------------
declare (strict_types=1);

namespace app;

use app\listener\wechat\OffcialAccountListener;
use app\listener\wechat\OpenPlatformListener;
use app\listener\wechat\RoutineListener;
use app\listener\wechat\WorkListener;
use app\services\work\WorkConfigServices;
use hdphp\services\SystemConfigService;
use hdphp\services\wechat\config\HttpCommonConfig;
use hdphp\services\wechat\config\LogCommonConfig;
use hdphp\services\wechat\config\WorkConfig;
use hdphp\services\wechat\MiniProgram;
use hdphp\services\wechat\OfficialAccount;
use hdphp\services\wechat\OpenPlatform;
use hdphp\services\wechat\Work;
use hdphp\utils\Json;
use think\Service;
use Yurun\Util\Swoole\Guzzle\SwooleHandler;
use GuzzleHttp\DefaultHandler;

/**
 * 应用服务类
 */
class AppService extends Service
{
    public function boot()
    {
        // 服务启动
        defined('DS') || define('DS', DIRECTORY_SEPARATOR);
//        DefaultHandler::setDefaultHandler(SwooleHandler::class);
    }

    public function register()
    {
        // 服务注册
        $this->app->bind('json', Json::class);
        $this->app->bind('sysConfig', SystemConfigService::class);

        // http配置服务
        $this->app->bind(HttpCommonConfig::class, function () {
            return (new HttpCommonConfig())->setServe(\app\services\system\config\SystemConfigServices::class);
        });

        // 公众号
        $this->app->bind(OfficialAccount::class, function () {
            return (new OfficialAccount)->setPushMessageHandler(OffcialAccountListener::class);
        });

        // 小程序
        $this->app->bind(MiniProgram::class, function () {
            return (new MiniProgram)->setPushMessageHandler(RoutineListener::class);
        });

        // 开放平台
        $this->app->bind(OpenPlatform::class, function () {
            return (new OpenPlatform)->setPushMessageHandler(OpenPlatformListener::class);
        });

        // 实例化企业微信配置
        $this->app->bind(WorkConfig::class, function () {
            return (new WorkConfig(new LogCommonConfig(), $this->app->make(HttpCommonConfig::class)))->setHandler(WorkConfigServices::class);
        });

        // 企业微信
        $this->app->bind(Work::class, function () {
            return (new Work)->setPushMessageHandler(WorkListener::class)
                ->setConfigHandler(WorkConfigServices::class);
        });
    }

}
